package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    public R<Page<SetmealDto>> findByPage(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> setmealDtoPage = setmealService.findByPage(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session) {
        Employee userLogin = (Employee) session.getAttribute("userLogin");
        setmealDto.setCreateUser(userLogin.getId());
        setmealDto.setUpdateUser(userLogin.getId());
        setmealService.save(setmealDto);
        return R.success("添加成功");
    }

    @GetMapping("/{id}")
    public R<SetmealDto> findById(@PathVariable long id) {
        SetmealDto setmealDto = setmealService.findById(id);
        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto, HttpSession session) {
        Employee userLogin = (Employee) session.getAttribute("userLogin");
        setmealDto.setUpdateUser(userLogin.getId());
        setmealService.update(setmealDto);
        return R.success("修改成功");
    }


    @GetMapping("/list")
    public R<List<Setmeal>> list(Long categoryId, Integer status) {
        List<Setmeal> setmealList =  setmealService.findAllByCidAndStatus(categoryId,status);
        return R.success(setmealList);
    }

    //起售停售
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@RequestParam List<Long> ids,@PathVariable Integer status){
        setmealService.updateStatus(ids,status);
        return R.success("在售状态修改成功");
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.delete(ids);
        return R.success("删除成功");
    }
}
